package ai.people.netmon.telegram.controller;

//import ai.people.core.security.support.RequestHolderUtil;
import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.businessenum.AnalysisTypeEnum;
import ai.people.netmon.framework.domain.telegram.entity.AnalysisDataUser;
import ai.people.netmon.framework.domain.telegram.entity.Channels;
import ai.people.netmon.framework.domain.telegram.vo.channels.AnalyzedChannels;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChatMessage;
import ai.people.netmon.framework.domain.telegram.vo.channels.SimilarChannels;
import ai.people.netmon.framework.domain.telegram.vo.channels.UserMember;
import ai.people.netmon.framework.model.response.ListResponse;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.telegram.domain.request.AnalyzedListRequest;
import ai.people.netmon.telegram.domain.request.JoinAnalysisRequest;
import ai.people.netmon.telegram.domain.request.channels.AccountMemberRequest;
import ai.people.netmon.telegram.domain.request.channels.ChartMessageRequest;
import ai.people.netmon.telegram.domain.request.channels.KeyGoalsRequest;
import ai.people.netmon.telegram.domain.request.channels.SimilarChannelsRequest;
import ai.people.netmon.telegram.service.ChannelsAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 群组分析
 *
 * @author yuanqinglong
 * @date 2022/1/11 19:23
 */
@Validated
@RestController
@RequestMapping("/channels/analysis")
public class ChannelsAnalysisController {

    @Autowired
    private ChannelsAnalysisService channelsAnalysisServiceImpl;

    /**
     * 添加群组
     *
     * @param urlOrId url或id
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> addChannels(String urlOrId) {
        channelsAnalysisServiceImpl.addChannels(urlOrId);
        return ResultUtil.success();
    }


    /**
     * 加入分析
     *
     * @param joinAnalysisRequest 分析数据用户
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/joinAnalysis")
    public Result<?> joinAnalysis(@Valid @RequestBody JoinAnalysisRequest joinAnalysisRequest) {
        AnalysisDataUser analysisDataUser = new AnalysisDataUser(joinAnalysisRequest.getAnalysisTargetId(), joinAnalysisRequest.getAnalysisTargetName());
        analysisDataUser.setAnalysisType(AnalysisTypeEnum.CHANNELS.getCode());
        analysisDataUser.setAnalysisSource(AnalysisSourceEnum.telegram.name());
        analysisDataUser.setCreateDate(LocalDateTime.now());
        //analysisDataUser.setUserId(RequestHolderUtil.getUserId());
        channelsAnalysisServiceImpl.joinAnalysis(analysisDataUser);
        return ResultUtil.success();
    }


    /**
     * 查询群组基本信息
     *
     * @param id id
     * @return {@link Result}<{@link Channels}>
     */
    @GetMapping("/{id}")
    public Result<Channels> queryChannels(@PathVariable Long id) {
        Channels channels = channelsAnalysisServiceImpl.getChannels(id);
        return ResultUtil.success(channels);
    }


    /**
     * 成员列表
     *
     * @param accountMemberRequest 参数
     * @return {@link Result}<{@link PageResponse}<{@link UserMember}>>
     */
    @PostMapping("/members")
    public Result<PageResponse<UserMember>> memberList(@Valid @RequestBody AccountMemberRequest accountMemberRequest) {
        accountMemberRequest.setAnalysisSource(AnalysisSourceEnum.telegram.name());
        PageResponse<UserMember> memberList = channelsAnalysisServiceImpl.memberList(accountMemberRequest);
        return ResultUtil.success(memberList);
    }


    /**
     * 重要目标
     *
     * @param keyGoalsRequest 重要目标参数
     * @return {@link Result}
     */
    @PostMapping("/keyGoals")
    public Result<?> keyGoals(@Valid @RequestBody KeyGoalsRequest keyGoalsRequest) {
        // TODO: 2022/2/10 重点目标

        return ResultUtil.success();
    }


    /**
     * 聊天信息
     *
     * @param chartMessageRequest 聊天信息参数
     * @return {@link Result}<{@link PageResponse}<{@link ChatMessage}>>
     */
    @PostMapping("/chatMessage")
    public Result<PageResponse<ChatMessage>> chatMessage(@Valid @RequestBody ChartMessageRequest chartMessageRequest) {
        return ResultUtil.success(channelsAnalysisServiceImpl.chatMessage(chartMessageRequest));
    }


    /**
     * 同类群组
     *
     * @param similarChannelsRequest 同类群组
     * @return {@link Result}<{@link ListResponse}<{@link SimilarChannels}>>
     */
    @PostMapping("/similarChannels")
    public Result<ListResponse<SimilarChannels>> similarChannels(@Valid @RequestBody SimilarChannelsRequest similarChannelsRequest) {
        List<SimilarChannels> similarChannels = channelsAnalysisServiceImpl.similarChannels(similarChannelsRequest);
        ListResponse<SimilarChannels> channelsListResponse = ListResponse.builder(similarChannels);
        return ResultUtil.success(channelsListResponse);
    }


    /**
     * 已添加分析群组
     *
     * @return {@link Result}<{@link ListResponse}<{@link AnalyzedChannels}>>
     */
    @PostMapping("/analyzedChannels")
    public Result<ListResponse<AnalyzedChannels>> addedAnalysisChannels() {
        AnalyzedListRequest analyzedListRequest = new AnalyzedListRequest(null, AnalysisTypeEnum.CHANNELS.getCode(), AnalysisSourceEnum.telegram.name());
        List<AnalyzedChannels> addedAnalysisChannels = channelsAnalysisServiceImpl.analyzedChannels(analyzedListRequest);
        ListResponse<AnalyzedChannels> channelsListResponse = ListResponse.builder(addedAnalysisChannels);
        return ResultUtil.success(channelsListResponse);
    }


}
